/*     */ package com.lmpop.trade.util;
/*     */ 
/*     */ import com.lmpop.frame.util.Config;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.util.Hashtable;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentException;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.Node;
/*     */ import org.dom4j.io.SAXReader;
/*     */ import org.dom4j.io.XMLWriter;
/*     */ 
/*     */ public class ParseXmlUtil
/*     */ {
/*  25 */   private static ParseXmlUtil parseXmlUtil = null;
/*     */ 
/*  27 */   private Document document = null;
/*     */ 
/*  29 */   private String basePath = new Config().getString("rootpath");
/*     */ 
/*     */   public static ParseXmlUtil getInstance()
/*     */   {
/*  36 */     if (parseXmlUtil == null) {
/*  37 */       synchronized (ParseXmlUtil.class) {
/*  38 */         if (parseXmlUtil == null) {
/*  39 */           parseXmlUtil = new ParseXmlUtil();
/*     */         }
/*     */       }
/*     */     }
/*  43 */     return parseXmlUtil;
/*     */   }
/*     */ 
/*     */   public ParseXmlUtil init(String xmlPath)
/*     */   {
/*  48 */     SAXReader reader = new SAXReader();
/*     */     try
/*     */     {
/*  51 */       this.document = reader.read(new File(xmlPath));
/*     */     }
/*     */     catch (DocumentException e) {
/*  54 */       e.printStackTrace();
/*     */     }
/*     */ 
/*  57 */     return getThis();
/*     */   }
/*     */ 
/*     */   public Hashtable getAttribute(String nodeName) {
/*  61 */     Hashtable attributesMap = new Hashtable();
/*  62 */     String elementName = "/quartz/job[@id='" + nodeName + "']";
/*     */ 
/*  64 */     Node root = getNode(this.document, elementName);
/*  65 */     System.out.println("Debug :" + root.getName());
/*  66 */     List childNodes = null;
/*  67 */     if (root != null) {
/*  68 */       childNodes = root.selectNodes("trigger/cron");
/*     */ 
/*  70 */       Iterator iterator = childNodes.iterator();
/*  71 */       if (iterator.hasNext()) {
/*  72 */         Element element = (Element)iterator.next();
/*  73 */         List elementList = element.elements();
/*  74 */         int size = elementList.size();
/*     */ 
/*  76 */         Iterator elementIterator = elementList.iterator();
/*  77 */         while (elementIterator.hasNext()) {
/*  78 */           Element childElement = (Element)elementIterator.next();
/*  79 */           attributesMap.put(childElement.getName(), childElement.getText());
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/*  84 */     return attributesMap;
/*     */   }
/*     */ 
/*     */   public void updateAttribute(String nodeName, Map updateMap)
/*     */   {
/*  91 */     String elementName = "/quartz/job[@id='" + nodeName + "']";
/*  92 */     String filePath = this.basePath + "WEB-INF/classes/quartz_job.xml";
/*     */ 
/*  94 */     Node root = getNode(this.document, elementName);
/*  95 */     System.out.println("Debug :" + root.getName());
/*  96 */     List childNodes = null;
/*  97 */     if (root != null) {
/*  98 */       childNodes = root.selectNodes("trigger/cron");
/*     */ 
/* 100 */       Iterator iterator = childNodes.iterator();
/* 101 */       if (iterator.hasNext()) {
/* 102 */         Element element = (Element)iterator.next();
/* 103 */         List elementList = element.elements();
/* 104 */         int size = elementList.size();
/*     */ 
/* 106 */         Iterator elementIterator = elementList.iterator();
/* 107 */         while (elementIterator.hasNext()) {
/* 108 */           Element childElement = (Element)elementIterator.next();
/*     */ 
/* 116 */           if (updateMap.get(childElement.getName()) != null) {
/* 117 */             childElement.setText(updateMap.get(childElement.getName()).toString());
/*     */           }
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 125 */     XMLWriter writer = new XMLWriter();
/*     */     try {
/* 127 */       writer.setOutputStream(new FileOutputStream(new File(filePath)));
/* 128 */       writer.write(this.document);
/* 129 */       writer.close();
/*     */     }
/*     */     catch (UnsupportedEncodingException e) {
/* 132 */       e.printStackTrace();
/*     */     }
/*     */     catch (FileNotFoundException e) {
/* 135 */       e.printStackTrace();
/*     */     }
/*     */     catch (IOException e) {
/* 138 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public Node getNode(Document doc, String elementName)
/*     */   {
/* 144 */     Node node = doc.selectSingleNode(elementName);
/*     */ 
/* 146 */     return node;
/*     */   }
/*     */ 
/*     */   public ParseXmlUtil getThis() {
/* 150 */     return this;
/*     */   }
/*     */ }

/* Location:           F:\电商\commons-utils.jar
 * Qualified Name:     com.lmpop.trade.util.ParseXmlUtil
 * JD-Core Version:    0.6.0
 */